const BASE_URL = "https://www.xlshhr.com/beauty-industry";
// const BASE_URL = "https://test.xlshhr.com/beauty-industry";

// class Request {

    
// }


/**
 * Request请求方法
 * @param  {String} url    链接
 * @param  {Objece} params 参数
 * @param  {Boolean} isToken  是否携带token
 * @return {Promise}       包含抓取任务的Promise
 */
function getApi(url, params, isToken, isbinding,isshowerror) {

    if (isToken === undefined) isToken = true;
    if (isbinding === undefined) isbinding = true;
    if (isshowerror === undefined) isshowerror = false;
    

    let token = wx.getStorageSync('token') || '';
    let uKey = wx.getStorageSync('uKey') || '';

    const promise = new Promise((resolve, reject) => {
        //判断是否需要置入token
        if (isToken) params = Object.assign({sessionId:token}, params)
        wx.request({
            url: `${BASE_URL}${url}`,
            method: 'POST',
            data: params, //置入token
            header: { 'Content-Type': 'application/x-www-form-urlencoded', 'token': token, 'uKey': uKey },
            success: res => {
                console.log(res);
                
                if (res.statusCode == 200) {
                    if(res.data.code ==1){
                        resolve(res);
                    }else if(res.data.code ==2){
                        wx.navigateTo({
                            url: '/pages/login/index',
                        })
                    }else{
                        wx.showToast({
                            title: res.data.desc,
                            icon:'none',
                            duration:800
                        })
                    }
                    

                } else {
                    wx.showToast({
                        title: '[' + res.code + '] 服务器出错,请重试',
                        icon: 'none',
                        duration:800
                    });
                    reject();
                }
            },
            fail: err => {
                wx.showToast({
                    title: '网络错误',
                    icon: 'none',
                    duration:800
                });
                reject();
            },
            complete: res => {

            }
        });
    });

    if (token == '' && isToken) {
        return new Promise((resolve, reject) => {
            wx.showToast({
                title: '状态失效，请关闭小程序后，重新打开',
                icon: 'none'
            });
        });
    } else {
        return promise;
    }
}

function requestApi(url, params) {
    params = params ||{};
    let sessionId = wx.getStorageSync('token');
    let shopId = wx.getStorageSync('shopId');
    if(!params.sessionId){
        params.sessionId = sessionId;
    }
    if(!params.shopId){
        params.shopId = shopId;
    }
    
    return new Promise((resolve, reject) => {
        getApi(url,params).then((res) => {
            resolve(res.data.data,res.data);
        },(res) => {
            reject(res.data);
        });
    });
}



module.exports = {
    get: getApi,
    post: getApi,
    requestApi:requestApi,
    url: BASE_URL
}

// let request = new Request();

// module.exports = {
//     get: request.getApi,
//     post: request.getApi,
//     requestApi:request.requestApi,
//     url: BASE_URL
// }